$(function(){
    $("#event_method_lists li").click(function(){
        var event_method = $(this).attr("data");
        $("#event_method").val(event_method);
        $("#event_method_lists li").removeClass("active");
        $(this).addClass("active");
    });

    $("#submit_type_lists li").click(function(){
        var value = $(this).attr("data");
        $("#submit_type").val(value);
        $("#submit_type_lists li").removeClass("active");
        $(this).addClass("active");
        if (value == 1) {
            $(".form-more-setting").show();
        } else {
            $(".form-more-setting").hide();
            $("#siteBackMoreSetting").hide();
        }
    });

    $("#submit_browser_parent_lists li").click(function(){
        var value = $(this).attr("data");
        $("#submit_browser_parent_lists li").removeClass("active");
        $(this).addClass("active");
        if (value == "detial") {
            $("#submit_browser_detial_div").show();
            $("#submit_browser_lists li").addClass("active");
        } else {
            $("#submit_browser").val("[]");
            $("#submit_browser_detial_div").hide();
        }
    });

    $("#submit_browser_lists li").click(function(){
        var thisClass = $(this).attr("class");
        if (thisClass == "active") {
            $(this).removeClass("active");
        } else {
            $(this).addClass("active");
        }
        var submitBrowser = [];
        $("#submit_browser_lists .active").each(function ( index,element){
            submitBrowser.push($(this).attr("data"));
        });
        $("#submit_browser").val(JSON.stringify(submitBrowser));
        if (submitBrowser.length <= 0) {
            $("#submit_browser_detial_div").hide();
            $("#submit_browser_parent_lists li").eq(0).addClass("active");
            $("#submit_browser_parent_lists li").eq(1).removeClass("active");
        }
    });

    $("#event_active_lists li").click(function(){
        var event_active = $(this).attr("data");
        $("#event_active_lists li").removeClass("active");
        $(this).addClass("active");
        $("#event_active").val(event_active);
    });
});

$(document).ready(function(){
    var copylink = new ClipboardJS('#copylink');
    copylink.on('success', function(e) {
        var name = $("#copylink").attr("name");
        mviewToastr("success",name+"复制成功，请粘贴获取链接!");
    });
    renderSiteListBackSetting();
    rederEventActive();
});

function rederEventActive() {
    var event_active = $("#event_active").val();
    $("#event_active_lists li").removeClass("active");
    $("#event_active_lists li[data="+event_active+"]").addClass("active");
}

function siteListsbackMore() {
    if ($("#wxkfListBackMore i").hasClass("bx-chevrons-down")) {
        $("#wxkfListBackMore i").attr("class","bx bx-chevrons-up");
        $("#wxkfListBackMore p").text("关闭更多专业配置");
    } else {
        $("#wxkfListBackMore i").attr("class","bx bx-chevrons-down");
        $("#wxkfListBackMore p").text("更多专业配置");
    }
    $("#siteBackMoreSetting").slideToggle();
}

function renderSiteListBackSetting() {
    var siteListsBack = JSON.parse($("#site_lists_back").val());
    $("#submit_rate").val(siteListsBack.submit_rate);
    $("#submit_time").val(siteListsBack.submit_time);
    $("#transform_target").val(siteListsBack.transform_target);
    $("#assets_id").val(siteListsBack.assets_id);
    $("#other").val(siteListsBack.other);
    $("#kfid").val(siteListsBack.kfid);
    $("#back_platfrom").val(siteListsBack.back_platfrom);
    if (siteListsBack.back_platfrom == "oppo" || siteListsBack.back_platfrom == "vivo") {
        $("#api_id").val(siteListsBack.api_id);
        $("#api_key").val(siteListsBack.api_key);
    }
    $("#submit_type").val(siteListsBack.submit_type);
    if (siteListsBack.submit_type == 1) {
        $("#submit_type_div li").eq(0).addClass("active");
        $("#submit_type_div li").eq(1).removeClass("active");
        $(".form-more-setting").show();
    } else {
        $("#submit_type_div li").eq(1).addClass("active");
        $("#submit_type_div li").eq(0).removeClass("active");
        $(".form-more-setting").hide();
        $("#siteBackMoreSetting").hide();
    }

    $("#submit_browser").val(siteListsBack.submit_browser);
    var submitBrowsers = JSON.parse(siteListsBack.submit_browser);
    if (submitBrowsers.length <= 0) {
        $("#submit_browser_detial_div").hide();
        $("#submit_browser_parent_lists li").eq(0).addClass("active");
        $("#submit_browser_parent_lists li").eq(1).removeClass("active");
    } else {
        siteListsbackMore();
        $("#submit_browser_detial_div").show();
        $("#submit_browser_lists li").removeClass("active");
        $("#submit_browser_parent_lists li").eq(0).removeClass("active");
        $("#submit_browser_parent_lists li").eq(1).addClass("active");
        $.each(submitBrowsers,function(key,submitBrowser){
            $("#submit_browser_lists li[data='"+submitBrowser+"']").addClass("active");
        });
        $("#submit_browser").val(JSON.stringify(submitBrowsers));
    }
    transformTargetChange();
}

function transformTargetChange() {
    var transform_target = $("#transform_target").val();
    var transform_target_text = $("#transform_target").find("option:selected").text();
    var back_platfrom = $("#back_platfrom").val();
    if (back_platfrom == "dy") {
        $("#event_admin_notice").text(transform_target_text+"(英文名"+transform_target+")");
    } else {
        $("#event_admin_notice").text(transform_target_text);
    }
}

function eventChange(key) {
    var value = $("#"+key).val();
    var eventLists = {"page_view":"访问目标页面","wechat_qrcode_show":"微信_二维码展示","wechat":"微信复制","wechat_qrcode_try":"微信_长按二维码","work_wechat_added":"微信_添加企业微信","customer_effective":"有效获客","consult_effective":"有效咨询","game_addiction":"关键行为","active_pay":"付费","work_wechat_confirm":"微信_用户确认意向","first_pay":"首购","successful_pay":"支付成功"};
    $("#"+key+"_text").text(eventLists[value]);
}

function confirmMviewNotification() {
    colseMviewNotification();
    var mviewNotificationData = JSON.parse($("#mviewNotificationData").val());
    console.log(mviewNotificationData);
    if (mviewNotificationData.type == "copy") {
        var debug_time = $("#debug_time").val();
        var kfid = $("#kfid").val();
        var debug_sign = $("#debug_sign").val();
        var back_platfrom = $("#back_platfrom").val();
        var users_domain = $("#users_domain").val();
        if (mviewNotificationData.version == "debug") {
            var debugUrl = users_domain+'/kf/'+kfid+'?debug_time='+debug_time+'&debug_sign='+debug_sign+'&back_platfrom='+back_platfrom;
            if (back_platfrom == "bili") {
                debugUrl = debugUrl+'&trackid=__TRACKID__&accountid=__ACCOUNTID__&campaignid=__CAMPAIGNID__';
            } else if (back_platfrom == "zh") {
                debugUrl = debugUrl+'&cb=__CALLBACK__';
            } else if (back_platfrom == "xmly") {
               debugUrl = debugUrl+'&task_id=_TASK_ID_&plan_id=_PLAN_ID_&material_id=_MATERIAL_ID_&callback=_CALLBACK_URL_';
            } else if (back_platfrom == "ks") {
               debugUrl = debugUrl+'&aid=__AID__';
            }
            copyLinks(debugUrl,'联调测试链接');
        } else {
            var onlineUrl = users_domain+'/kf/'+kfid+'?back_platfrom='+back_platfrom;
            if (back_platfrom == "xmly") {
                onlineUrl = onlineUrl+"&callback=_CALLBACK_URL_";
            } else if (back_platfrom == "zh") {
                onlineUrl = onlineUrl+'&cb=__CALLBACK__';
            } else if (back_platfrom == "bili") {
                onlineUrl = onlineUrl+'&trackid=__TRACKID__&accountid=__ACCOUNTID__&campaignid=__CAMPAIGNID__';
            } else if (back_platfrom == "ks") {
                onlineUrl = onlineUrl+'&aid=__AID__';
            }
            copyLinks(onlineUrl,'推广链接');
        }
    } else {
        siteBackUpdateSubmit();
    }
}

function copyLinks(data,message) {
    $("#copylink").attr("data-clipboard-text",data);
    $("#copylink").attr("name",message);
    $("#copylink").trigger("click");
}

function siteBackUpdateSubmit() {
    var kfid = $("#kfid").val();
    var back_platfrom = $("#back_platfrom").val();
    var assets_id = $("#assets_id").val();
    var submit_rate = $("#submit_rate").val();
    var transform_target = $("#transform_target").val();
    if (transform_target == "" || transform_target === null) {
        mviewToastr("error","请选择转化目标!");
        return;
    }
    var submit_type = $("#submit_type").val();
    var api_id = $("#api_id").val();
    var api_key = $("#api_key").val();
    var event_active = $("#event_active").val();
    var submit_time = $("#submit_time").val();
    var submit_browser = $("#submit_browser").val();
    var other = $("#other").val();
    if (transform_target == "") {
        mviewToastr("error","请选择转化目标!");
        return;
    }
    if (event_active == "[]") {
        mviewToastr("error","请选择触发回调的操作!");
        return;
    }
    if (back_platfrom == "dy" && assets_id.length < 15) {
        mviewToastr("error","请输入正确的落地页站点ID!");
        return;
    }
    if (back_platfrom == "oppo" && assets_id.length != 10) {
        mviewToastr("error","请输入正确的广告主ID!");
        return;
    }
    if (back_platfrom == "oppo" && api_id.length != 32) {
        mviewToastr("error","请输入正确的API_ID!");
        return;
    }
    if (back_platfrom == "oppo" && api_key.length != 32) {
        mviewToastr("error","请输入正确的API_KEY!");
        return;
    }
    if (back_platfrom == "bd" && other.length != 65) {
        mviewToastr("error","请输入正确的API回调Token!");
        return;
    }
    if (back_platfrom == "zh" || back_platfrom == "xmly") {
        var event_method = $("#event_method").val();
        var event_method_value = $("#event_method_value").val();
        eval('var '+event_method+'="'+event_method_value+'";');
    }
    if (back_platfrom == "xmly") {
        var event_method_tow = $("#event_method_tow").val();
        var event_method_tow_value = $("#event_method_tow_value").val();
        eval('var '+event_method_tow+'="'+event_method_tow_value+'";');
    }
    var domain = $("#users_domain").val();
    if (submit_rate > 100 || submit_rate <= 0) {
        mviewToastr("error","回传比例必须为1-100之间!");
        return;
    }
    if (submit_time >= 86400) {
        mviewToastr("error","转化时长过滤时间不能超过86400秒(1天)!");
        return;
    }
    mviewToastr("load","提交中,请稍等...");
    $.ajax({
        url: "/admin/wxkf/work_link_back_submit",
        type: "POST",
        data: {
            "kfid":kfid,
            "back_platfrom":back_platfrom,
            "assets_id":assets_id,
            "submit_rate":submit_rate,
            "transform_target":transform_target,
            "submit_type":submit_type,
            "event_active":event_active,
            "other":other,
            "submit_time":submit_time,
            "submit_browser":submit_browser,
            "api_id":api_id,
            "api_key":api_key,
            "domain":domain,
        },
        headers: {
            'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
        },
        success:function(data){
            if (data.status == "success") {
                mviewToastr("success",data.msg);
                if (back_platfrom == "vivo") {
                    var vivoAuth = $("#vivoAuth").val();
                    if (vivoAuth == 1) {
                        window.location.href = "https://open-ad.vivo.com.cn/OAuth?clientId="+api_id+"&state=duoyue&redirectUri="+encodeURIComponent("https://dy.moreqifu.cn/api/version/1/sem/vivo_auth_code?kfid="+kfid);
                    }
                }
                // setTimeout(function(){
                //     window.location.href = "/admin/hkzs/hkzs_detial/"+kfid;
                // }, 2000);
            } else {
                mviewToastr("error",data.msg);
            }
        },
        error:function(err){
            if (err.status == 419) {
                mviewToastr("error","页面已过期,请刷新网页后重新保存！");
            } else {
                mviewToastr("error","网络异常,请联系客服处理！");
            }
        }
    })
}
